import Foundation

struct Quote: Codable {
    let text: String
    let author: String
}

class NetworkService {
    
    static let shared = NetworkService()
    
    private init() {}
    
    func fetchQuote(completion: @escaping (Result<String, Error>) -> Void) {
        guard let url = URL(string: "https://betasystem.gsf.inner.netease.com/mytest/1231231231231.txt") else {
            completion(.failure(NSError(domain: "", code: -1, userInfo: [NSLocalizedDescriptionKey: "Invalid URL"])))
            return
        }
        
        URLSession.shared.dataTask(with: url) { data, response, error in
            if let error = error {
                completion(.failure(error))
                return
            }
            
            guard let data = data else {
                completion(.failure(NSError(domain: "", code: -2, userInfo: [NSLocalizedDescriptionKey: "No data received"])))
                return
            }
            
            do {
                let string = try String(data: data, encoding: .utf8)
                completion(.success(string!))
            } catch {
                completion(.failure(error))
            }
        }.resume()
    }
}
